package com.stlm2.dems.service;

import com.stlm2.dems.dao.GoodsOrderDao;
import com.stlm2.dems.dao.ShopOrderDao;
import com.stlm2.dems.entity.*;
import com.stlm2.util.Convert;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;

import com.stlm2.dems.dao.ServiceOrderDao;
import com.stlm2.core.base.BaseDao;
import com.stlm2.core.base.impl.BaseServiceImpl;
import org.springframework.ui.ModelMap;

import java.util.ArrayList;
import java.util.List;

@Service("serviceOrderService")
@Transactional(rollbackForClassName={"java.lang.Exception"}, propagation = Propagation.REQUIRED)
public class ServiceOrderService extends BaseServiceImpl<ServiceOrder> {

	@Resource
	private ServiceOrderDao serviceOrderDao;
	@Resource
	private ValueAddedServicesService valueAddedServicesService;
	@Resource
	private ShopOrderService shopOrderService;
	@Resource
	private GoodsOrderService goodsOrderService;
	@Resource
	private DdmShopService ddmShopService;
	@Resource
	private DdmGoodsService ddmGoodsService;

	@Resource
	private ShopOrderDao shopOrderDao;
	@Resource
	private GoodsOrderDao goodsOrderDao;

	@Override
	protected BaseDao <ServiceOrder> getDao() {
		return serviceOrderDao;
	}

    public List<ServiceOrder> findEntityByUserID(Integer userId) {
		return serviceOrderDao.findEntityByUserID(userId);
    }

    public List<ModelMap> convert(List<ServiceOrder> serviceOrders) {
		List<ModelMap> list = new ArrayList<ModelMap>();
		if (serviceOrders != null && serviceOrders.size() > 0) {
			for (ServiceOrder serviceOrder : serviceOrders) {
				ModelMap map = buildModel(serviceOrder);
				list.add(map);
			}
		}
		return list;
	}
	public ModelMap buildModel(ServiceOrder serviceOrder) {
		ModelMap model = new ModelMap();
		if(serviceOrder!=null) {
			ValueAddedServices valueAddedServices=valueAddedServicesService.findEntityById(serviceOrder.getServiceId());
			ShopOrder shopOrder=shopOrderDao.getByCode(serviceOrder.getShopOrderCode());
			DdmShop ddmShop=ddmShopService.findEntityById(shopOrder.getShopId());
			GoodsOrder goodsOrder=goodsOrderDao.getByCode(serviceOrder.getGoodsOrderCode());
			DdmGoods ddmGoods=ddmGoodsService.findEntityById(goodsOrder.getGoodsId());
			model.put("shopName",ddmShop.getName());
			model.put("goodName",ddmGoods.getName());
			model.put("name",valueAddedServices.getName()!=null ?valueAddedServices.getName():"");
			model.put("id", serviceOrder.getId());
			model.put("state",serviceOrder.getState());
			model.put("num",serviceOrder.getNum());
			model.put("createTime", Convert.toShortYMD(serviceOrder.getCreateTime()));
			model.put("endTime",Convert.toShortYMD(serviceOrder.getEndTime()));
			model.put("imie",serviceOrder.getImie()!=null?serviceOrder.getImie():"");
		}
		return  model;
	}
}